$(function(){
    initMenu();

    $(".menuListGroup").bind("mouseover",overFirstLevel);
    $(".menuListGroup").bind("mouseout",outFirstLevel);
    $(".menuListGroup>.menuList>li a").bind("click",clickSecondLevel);

    function initMenu(){
        $(".menuListGroup").removeClass("open");
        $(".menuListGroup>.menuPrimary>li").removeClass("caretTop");
        $(".menuListGroup>.menuPrimary>li").addClass("caretBottom");

        $(".menuListGroup.cur").addClass("open");
        $(".menuListGroup.cur>.menuPrimary>li").removeClass("caretBottom");
        $(".menuListGroup.cur>.menuPrimary>li").addClass("caretTop");
    }

    function overFirstLevel(event){
        var curMenuGroup = $(event.currentTarget);
        if(!curMenuGroup.hasClass("cur")){
            curMenuGroup.addClass("cur");
            checkMenuCur(curMenuGroup,true);
        }
    }

    function outFirstLevel(event){
        var curMenuGroup = $(event.currentTarget);
        checkMenuCur(curMenuGroup,false);
    }

    function clickSecondLevel(event){
        var curMenuGroup = $(event.currentTarget);
        location.href = curMenuGroup.attr("link");
    }

    function checkMenuCur(menuGroup,notCur){
        if($(menuGroup).hasClass("cur")){
            $(menuGroup).addClass("open");
            $(menuGroup).find(".menuPrimary>li").removeClass("caretBottom");
            $(menuGroup).find(".menuPrimary>li").addClass("caretTop");
        }else {
            $(menuGroup).removeClass("open");
            $(menuGroup).find(".menuPrimary>li").removeClass("caretTop");
            $(menuGroup).find(".menuPrimary>li").addClass("caretBottom");
        }
        if(notCur){
            $(menuGroup).removeClass("cur");
        }
    }
});